// Copyright (C) 2025 Kumo inc.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include "test.hpp"

#include <rothar/pegtl/contrib/instantiate.hpp>

namespace rothar
{
   bool ctor = false;
   bool dtor = false;

   struct test_class
   {
      template< typename ParseInput >
      test_class( const ParseInput& /*unused*/ )
      {
         ROTHAR_PEGTL_TEST_ASSERT( ctor == false );
         ROTHAR_PEGTL_TEST_ASSERT( dtor == false );

         ctor = true;
      }

      test_class( test_class&& ) = delete;
      test_class( const test_class& ) = delete;

      ~test_class()
      {
         ROTHAR_PEGTL_TEST_ASSERT( ctor == true );
         ROTHAR_PEGTL_TEST_ASSERT( dtor == false );

         dtor = true;
      }

      test_class& operator=( test_class&& ) = delete;
      test_class& operator=( const test_class& ) = delete;
   };

   using test_grammar = sor< alpha, digit >;

   template< typename Rule >
   struct test_action
      : nothing< Rule >
   {};

   template<>
   struct test_action< alpha >
   {
      static void apply0()
      {
         ROTHAR_PEGTL_TEST_ASSERT( ctor == true );
         ROTHAR_PEGTL_TEST_ASSERT( dtor == false );
      }
   };

   template<>
   struct test_action< sor< alpha, digit > >
      : instantiate< test_class >
   {};

   void unit_test()
   {
      memory_input in( "a", __FUNCTION__ );
      ROTHAR_PEGTL_TEST_ASSERT( parse< test_grammar, test_action >( in ) );

      ROTHAR_PEGTL_TEST_ASSERT( ctor == true );
      ROTHAR_PEGTL_TEST_ASSERT( dtor == true );
   }

}  // namespace rothar

#include "main.hpp"
